package com.vf.admin.client.controller.ip;

import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.service.AppIpAuthService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.miaodong.entity.AppIpAuth;
import com.vf.admin.utils.ip.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title : </p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : NemoMeng
 * @Date 2023/12/28 17:30
 */
@Controller
@RequestMapping(value = "/api/white")
public class IpWitheController extends ClientBaseController {

    @Resource
    private AppIpAuthService appIpAuthService;

    //审核地址
    String test = "http://uaitest.nhfaqw.com";
    //正式
    String prod = "http://api.ptszyxx.com/";


    @RequestMapping("/reqAddress")
    @ResponseBody
    public Result reqAddress(String account) {
        String ipAddress = IpUtils.getIpAddress(request);
        System.out.println("ipAddress"+ipAddress);
        boolean flag = false;
        if (ObjectUtil.isEmpty(ipAddress)) {
            flag = true;
        } else {
            List<AppIpAuth> ipList = appIpAuthService.getStartWithIpList(ipAddress);
            if(ObjectUtil.isNotEmpty(account)){
                List<AppIpAuth> accountList = appIpAuthService.getAccountList(account);
                if(ObjectUtil.isNotEmpty(accountList)){
                    flag = true;
                }
            }
            if(ObjectUtil.isNotEmpty(ipList)){
                flag = true;
            }

            if(!flag){
                boolean flag1 = IpUtils.isGuangzhou(ipAddress);
                boolean flag2 = IpUtils.isHuawei(ipAddress);
                if(flag1 || flag2){
                    flag = true;
                    try {
                        AppIpAuth appIpAuth = new AppIpAuth();
                        appIpAuth.setIp(ipAddress);
                        appIpAuth.setVersionName(request.getHeader("versionName"));
                        appIpAuth.setDeviceModel(request.getHeader("deviceModel"));
                        appIpAuth.setPackageName(request.getHeader("packageName"));
                        appIpAuth.setCreatetime(new Date());
                        appIpAuthService.save(appIpAuth);
                    }catch (Exception e){
                    }
                }
            }
        }

        if (flag) {
            Map<String, String> map2 = new HashMap<>();
            map2.put("url", test);
            map2.put("open","0");
            return this.sucessJSON(map2);
        } else {
            Map<String, String> map3 = new HashMap<>();
//            map3.put("url", prod);//正式
            map3.put("url", test);//审核
            map3.put("open","1");
            return this.sucessJSON(map3);
        }
    }

    @RequestMapping("/test")
    public void test() {
//        String ipAddress = IpUtils.getIpAddress(request);
//        System.out.println("test"+ipAddress);
//        List<AppIpAuth> ip = appIpAuthService.getIp(ipAddress);
//        System.out.println("AppIpAuth"+ip);
    }


}
